<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/14
 * Time: 18:50
 */

namespace App\Http\Controllers\api\activity;


use App\Http\Controllers\api\base\BaseController;
use App\Http\Requests\activity\ActivityRequest;
use App\Http\Resources\activity\ActivityAdminResource;
use App\Http\Resources\activity\ActivityApplyResource;
use App\Http\Resources\activity\ActivityDetailResource;
use App\Http\Resources\activity\ActivityPageResource;
use App\Http\Resources\activity\ActivitySignResource;
use App\Models\activity\Activity;
use App\Models\activity\ActivityAdmin;
use App\Models\activity\ActivityCate;
use App\Models\activity\ActivityMember;
use App\Models\activity\ActivitySign;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ActivityController extends BaseController
{

    /**
     * 活动管理人员
     * @param Request $request
     * @return mixed
     */
    public function activityAdmin(Request $request)
    {
        $data = ActivityAdmin::with('user')->where('status',1)->get();
        return success(new ActivityAdminResource($data));
    }

    /**
     * 活动分类列表
     * @param Request $request
     * @return mixed
     */
    public function cateList(Request $request)
    {
        $data = ActivityCate::where('status',1)->select(['id','name'])->get();
        return success($data);
    }

    /**
     * 活动列表
     * @param Request $request
     * @return mixed
     */
    public function activityList(Request $request)
    {
        $pageSize = $request->page_size;
        if(!$pageSize || !is_numeric($pageSize)){
            $pageSize = 10;
        }
        $cate_id = $request->cate_id;
        if($cate_id){
            $map[] = ['cate_id','=',$cate_id];
        }
        if($request->keyword){
            $map[] = ['title','like',"%{$request->keyword}%"];
        }
        $data = Activity::when($map,function ($query,$map){
            $query->where($map);
        })
            ->with('user')
            ->with('cate')
            ->orderBy('status','asc')
            ->orderByDesc('id')
            ->paginate($pageSize);
        return success(new ActivityPageResource($data));
    }

    /**
     * 活动详情
     * @param Request $request
     * @return mixed
     */
    public function activityDetail(Request $request)
    {
        $activity_id = $request->id;
        $data = Activity::with('user')->with('cate')->where(['id'=>$activity_id])->first();
        if(!$data){
            return error(204);
        }
        $adminList = ActivityAdmin::with('user')->where('status',1)->pluck('user_id');
        $data['join_status'] = ActivityMember::check($activity_id,$this->user_id);
        if (in_array($this->user_id,$adminList)) {
            $data['join_status'] = true;
        }
        $data['apply'] = ActivityMember::with('user')->where('activity_id',$activity_id)->get();
        $data['sign'] = ActivitySign::with('user')->where('activity_id',$activity_id)->get();
        return success(new ActivityDetailResource($data));
    }

    /**
     * 创建活动
     * @param ActivityRequest $request
     * @return mixed
     */
    public function createActivity(ActivityRequest $request)
    {
        $params = $request->only(['title','intro','start_time','end_time','apply_end_time','content']);

        if(!ActivityAdmin::check($this->user_id)){
            return error('当前用户不能发起活动');
        }
        $params['user_id'] = $this->user_id;
        $data = Activity::create($params);

        return success($data);
    }

    /**
     * 修改活动内容
     * @param Request $request
     * @return mixed
     */
    public function saveContent(Request $request)
    {
        $content = $request->content_text;
        $activity_id = $request->activity_id;
        $activity = Activity::where('id',$activity_id)->first();
        if(!$activity) {
            return error("活动不存在");
        }
        $activity->content = $content;
        $data = $activity->save();
        if(!$data) {
            return error("保存失败");
        }
        return success($activity);
    }

    /**
     * 报名参加活动
     * @param Request $request
     * @return mixed
     */
    public function apply(Request $request)
    {
        $id = $request->id;
        if(!is_numeric($id)){
            return error(205);
        }
        $map['id'] = $id;
        $activityMember = ActivityMember::where(['activity_id'=>$id,'user_id'=>$this->user_id])->first();
        if($activityMember){
            return error('您已参加此活动');
        }
        $data = Activity::where($map)->first();
        if(Carbon::now() > $data['apply_end_time']){
            return error('报名时间已经截止');
        }
        $params = [
            'activity_id' => $id,
            'user_id' => $this->user_id
        ];
        $data = ActivityMember::firstOrCreate($params);
        if($data){
            Activity::where('activity_id',$id)->increment('number',1);
        }
        return success($data);
    }

    /**
     * 活动签到
     * @param Request $request
     * @return mixed
     */
    public function sign(Request $request)
    {
        $id = $request->id;
        if(!is_numeric($id)){
            return error(205);
        }
        $map['id'] = $id;
        $activityMember = ActivityMember::where(['activity_id'=>$id,'user_id'=>$this->user_id])->first();
        if($activityMember){
            return error('您已签到');
        }
        $params = [
            'activity_id' => $id,
            'user_id' => $this->user_id
        ];
        $data = ActivityMember::firstOrCreate($params);
        return success($data);
    }

    /**
     * 报名列表
     * @param Request $request
     */
    public function applyList(Request $request)
    {
        $activity_id = $request->id;
        $data = ActivityMember::with('user')->where('activity_id',$activity_id)->get();
        return success(new ActivityApplyResource($data));
    }

    /**
     * 申请列表
     */
    public function signList(Request $request)
    {
        $activity_id = $request->id;
        $data = ActivitySign::with('user')->where('activity_id',$activity_id)->get();
        return success(new ActivitySignResource($data));
    }
}